<template>
  <el-dialog
    title="新增用户"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    destroy-on-close
    width="30%"
  >
    <div class="form">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-position="left"
        label-width="80"
        size="small"
      >
        <div class="avatar-panel">
          <div class="avatar-box">
            <el-form-item prop="username">
              <el-upload
                class="avatar-uploader"
                action="/file/upload"
                :show-file-list="false"
                name="avatar"
              >
                <img
                  v-if="form.avatar_path"
                  :src="form.avatar_path"
                  class="avatar"
                />
                <!-- <i v-else class="iconfont icon-touxiang avatar-uploader-icon" /> -->
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </div>
        </div>

        <el-row :gutter="10">
          <el-col :span="12">
            <quick-field-panel title="登录名" :required="true">
              <el-form-item prop="username">
                <el-input
                  v-model="form.username"
                  placeholder="请输入用户名"
                  clearable
                >
                </el-input>
              </el-form-item>
            </quick-field-panel>
          </el-col>

          <el-col :span="12">
            <quick-field-panel title="密码" :required="true">
              <el-form-item prop="password">
                <el-input
                  v-model="form.password"
                  placeholder="请输入密码"
                  clearable
                >
                </el-input>
              </el-form-item>
            </quick-field-panel>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <quick-field-panel title="真实姓名" :required="true">
              <el-form-item prop="name">
                <el-input
                  v-model="form.name"
                  placeholder="请输入真实姓名"
                  clearable
                >
                </el-input>
              </el-form-item>
            </quick-field-panel>
          </el-col>
          <el-col :span="12">
            <quick-field-panel title="电话号码" :required="true">
              <el-form-item prop="telephone">
                <el-input
                  v-model="form.telephone"
                  placeholder="请输入电话号码"
                  clearable
                >
                </el-input>
              </el-form-item>
            </quick-field-panel>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <quick-field-panel title="员工" :required="true">
              <el-form-item prop="employee_id">
                <el-select
                  v-model="form.employee_id"
                  placeholder="请选择"
                  clearable
                  filterable
                >
                  <el-option
                    v-for="item in employeeOpt"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </quick-field-panel>
          </el-col>

          <!-- <el-col :span="12">
            <quick-field-panel title="员工" :required="true">
              <el-form-item prop="name">
                <el-input
                  v-model="form.name"
                  placeholder="请输入真实姓名"
                  clearable
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-search icon-search"
                    @click="showEmployees"
                  ></i>
                </el-input>
              </el-form-item>
            </quick-field-panel>
          </el-col> -->
        </el-row>
        <!-- <quick-field-row title="所属终端" :required="true">
          <el-form-item prop="terminal">
            <el-radio-group v-model="form.terminal" size="mini">
              <el-radio :label="0">全部</el-radio>
              <el-radio :label="1">后台</el-radio>
              <el-radio :label="2">微信</el-radio>
            </el-radio-group>
          </el-form-item>
        </quick-field-row> -->

        <quick-field-panel title="备注">
          <el-form-item prop="remark">
            <el-input
              type="textarea"
              v-model="form.remark"
              placeholder="请输入备注"
              :rows="4"
              clearable
            >
            </el-input>
          </el-form-item>
        </quick-field-panel>
      </el-form>
    </div>
    <span slot="footer">
      <el-button type="primary" :loading="loading" size="medium" @click="doAdd"
        >确 定</el-button
      >
      <el-button size="medium" plain @click="isShow = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import _cryptojs from "crypto-js";
import _api from "@/common/api/user";
export default {
  data() {
    return {
      employeeOpt: [],
      loading: false,
      isShow: false,
      form: {
        is_authorize: false,
        avatar_path: "",
        employee_id: "",
        terminal: 1,
      },
      rules: {
        username: [{ required: true, message: "必填", trigger: "blur" }],
        name: [{ required: true, message: "必填", trigger: "blur" }],
        password: [{ required: true, message: "必填", trigger: "blur" }],
        telephone: [{ required: true, message: "必填", trigger: "blur" }],
        terminal: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },
  methods: {
    __init() {
      this.form = {
        is_authorize: false,
        avatar_path: "",
        terminal: 1,
      };
    },

    showDialog(employeeOpt) {
      this.employeeOpt = employeeOpt;
      this.__init();
      this.isShow = true;
    },

    showEmployees() {},

    async doAdd() {
      this.loading = true;
      try {
        await this.$refs.form.validate(async (valid) => {
          if (valid) {
            this.form.password = _cryptojs.MD5(this.form.password).toString();
            await _api.addAsync(this.form);
            this.isShow = false;
            this.$emit("added", {});
          }
        });
      } finally {
        this.loading = false;
      }
    },
  },
  mounted() {},
};
</script>

<style lang="scss" sooped>
.avatar-panel {
  display: flex;
  align-items: center;
  justify-content: center;

  margin: 10px 0;
}

.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    &:hover {
      border-color: #409eff;
    }
  }
}

.avatar-uploader .el-upload .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}

.icon-search {
  cursor: pointer;
}
</style>
